Completed
Push — master ( 6b3a87...e02f9a )
by Maxence
02:11
created

searchbox.searching   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 6.7272
c 1
b 0
f 0
cc 7
nc 6
nop 1
1
/*
2
 * FullTextSearch - Full text search framework for Nextcloud
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2018
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OCA */
27
/** global: nav */
28
/** global: _ */
29
/** global: api */
30
/** global: search */
31
/** global: result */
32
/** global: fullTextSearch */
33
/** global: settings */
34
35
36
var box_elements = {
37
	searchTimeout: null,
38
39
	searchBoxInitialized: false,
40
	search_form: null,
41
	moreDisplayed: false,
42
	iconSearchOptions: 'options.svg',
43
	iconSearch: 'fulltextsearch.svg',
44
	currentBackgroundImage: '',
45
46
	// v0.6.0
47
	searchInput: null,
48
	searchMore: null,
49
	delayAnimation: 500,
50
	divFullTextSearchIcon: null,
51
	divFullTextSearchPopup: null
52
};
53
54
55
var searchbox = {
56
57
	init: function () {
58
59
		var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
60
61
		// we remove old search
62
		var search_form = $('FORM.searchbox');
63
		if (search_form.length > 0) {
64
			search_form.remove();
65
		}
66
67
68
		var divHeaderRight = $('DIV.header-right');
69
		var divFullTextSearch = $('<div>', {id: 'fulltextsearch'});
70
		divHeaderRight.prepend(divFullTextSearch);
71
72
		box_elements.divFullTextSearchIcon = searchbox.generateFullTextSearchIcon();
73
		box_elements.divFullTextSearchPopup = searchbox.generateFullTextSearchPopup();
74
		divFullTextSearch.append(box_elements.divFullTextSearchIcon);
75
		divFullTextSearch.append(box_elements.divFullTextSearchPopup);
76
77
		OC.registerMenu(box_elements.divFullTextSearchIcon, box_elements.divFullTextSearchPopup,
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
			searchbox.displayedSearchPopup);
79
80
		fullTextSearch.retreiveOptions(settings.searchProviderId);
81
82
		$(window).bind('keydown', function (event) {
83
			if (event.ctrlKey || event.metaKey) {
84
				if (String.fromCharCode(event.which).toLowerCase() === 'f') {
85
					event.preventDefault();
86
					searchbox.displaySearchPopup(true);
87
				}
88
89
				return;
90
			}
91
92
			if (event.which === 27) {
93
				searchbox.displaySearchPopup(false);
94
			}
95
		});
96
97
98
	},
99
100
101
	generateFullTextSearchIcon: function () {
102
		var className = 'icon-fulltextsearch';
103
		if (!OCA.Theming.inverted) {
104
			className = 'icon-fulltextsearch-white';
105
		}
106
107
		var icon = $('<div>', {
108
			id: 'fts-icon',
109
			tabindex: 0,
110
			role: 'link',
111
			class: className + ' menutoggle'
112
		});
113
114
		icon.fadeTo(0, 0.6);
115
116
		return icon;
117
	},
118
119
120
	generateFullTextSearchPopup: function () {
121
		var popup = $('<div>', {
122
			id: 'fts-popup'
123
		});
124
125
		var self = this;
126
		box_elements.searchInput = $('<input>', {
127
			id: 'fts-input',
128
			placeholder: 'Search ' + settings.searchProviderName
129
		}).on('keyup', self.searching);
130
		box_elements.searchMore = $('<div>', {id: 'fts-more'});
131
132
		var divHeader = $('<div>', {id: 'fts-header'});
133
		divHeader.append($('<div>').append(box_elements.searchInput));
134
135
		popup.append(divHeader);
136
		popup.append(box_elements.searchMore);
137
138
		return popup;
139
	},
140
141
142
	displaySearchPopup: function (display) {
143
		if (display) {
144
			OC.showMenu(box_elements.divFullTextSearchIcon, box_elements.divFullTextSearchPopup,
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
145
				searchbox.displayedSearchPopup);
146
		} else {
147
			OC.hideMenus(null);
148
		}
149
	},
150
151
152
	displayedSearchPopup: function () {
153
		box_elements.searchInput.focus();
154
	},
155
156
157
	searching: function (force) {
158
		var search = box_elements.searchInput.val();
159
		if (force === undefined) {
160
			force = false;
161
		}
162
163
		if (!force && search.length < 3) {
164
			return;
165
		}
166
167
		if (search === '' || (!force && curr.lastRequest === search)) {
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
168
			return;
169
		}
170
		curr.lastRequest = search;
171
172
		api.search({
173
			providers: settings.searchProviderId,
174
			search: search,
175
			page: curr.page,
176
			options: searchbox.getSearchOptions(),
177
			size: 20
178
		});
179
180
//		return;
181
		// if (settings.lockSearchbox === true) {
182
		// 	return;
183
		// }
184
		// settings.lockSearchbox = true;
185
		// searchbox.search_icon.stop().fadeTo(100, 0);
186
		// searchbox.search_form.stop().fadeTo(100, 0.8);
187
		// searchbox.search_input.focus();
188
		// searchbox.search_icon_close.stop().fadeTo(200, 1);
189
		// if (settings.noMoreOptions) {
190
		// 	searchbox.search_icon_more.stop().fadeTo(200, 1);
191
		// }
192
	},
193
194
195
	onOptionsLoaded: function (result) {
196
		if (!result[settings.searchProviderId]) {
197
			return;
198
		}
199
200
		box_elements.searchMore.html(result[settings.searchProviderId]);
201
		box_elements.searchMore.find('INPUT').each(function () {
202
			$(this).on('change', function () {
203
				searchbox.searching(true);
204
			});
205
		})
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
206
	},
207
208
209
	/**
210
	 *
211
	 * 0.6.0
212
	 *
213
	 *
214
	 */
215
	//
216
	//
217
	// initFullTextSearchBox: function () {
218
	// 	if (box_elements.searchBoxInitialized) {
219
	// 		return;
220
	// 	}
221
	// 	var self = this;
222
	//
223
	// 	box_elements.search_input.unbind('keyup');
224
	// 	box_elements.search_input.bind('keyup blur change', function () {
225
	// 		if ($(this).val() === '') {
226
	// 			self.displaySearchOptionsIcon(false);
227
	// 		} else {
228
	// 			self.displaySearchOptionsIcon(true);
229
	// 		}
230
	//
231
	// 		self.searching();
232
	// 	});
233
	//
234
	// 	box_elements.searchBoxInitialized = true;
235
	// },
236
	//
237
238
239
	getSearchOptions: function () {
240
		var options = {};
241
242
		if (box_elements.searchMore === null) {
243
			return options;
244
		}
245
246
		box_elements.searchMore.find('INPUT').each(function () {
247
			var value = $(this).val();
248
249
			if ($(this).attr('type') === 'checkbox' && !$(this).is(':checked')) {
250
				value = '';
251
			}
252
253
			options[$(this).attr('id')] = value;
254
		});
255
256
		return options;
257
	},
258
259
260
	// TODO: do we really need this initSearch, or should we use the one from fulltextsearch.js !?
261
	initSearch: function (force) {
0 ignored issues
show
Unused Code introduced by
The parameter force is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
262
		// var search = searchbox.search_input.val();
263
		//
264
		// if (!force && search.length < 3) {
265
		// 	return false;
266
		// }
267
		//
268
		// if (curr.lastRequest === search) {
269
		// 	return true;
270
		// }
271
		//
272
		// curr.lastRequest = search;
273
		//
274
		// fullTextSearch.search({
275
		// 	providers: settings.searchProviderId,
276
		// 	search: search,
277
		// 	page: curr.page,
278
		// 	options: searchbar.getSearchOptions(),
279
		// 	size: 20
280
		// });
281
		//
282
		// return true;
283
	}
284
285
286
};
287
288
289